package com.rt.easyjava.base.modules.timerjob.service.job.jobStrategy.strategy;

import com.alibaba.fastjson.JSON;
import com.rt.tmpt.service.utils.SpringContextHolder;
import com.rt.easyjava.base.modules.timerjob.dto.TmptTimerBizDto;
import com.rt.easyjava.base.modules.timerjob.service.job.jobStrategy.JobStrategy;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.utils.others.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 存储过程
 */
@Slf4j
public class ProcedureJobStrategy extends JobStrategy {
    public ProcedureJobStrategy(TmptTimerBizDto tmptTimerBizDto,Result typeResult) {
        super.tmptTimerBizDto = tmptTimerBizDto;
        super.typeResult = typeResult;
    }

    @Override
    protected String job() throws Exception {
        String className = tmptTimerBizDto.getClassName();
        String param = tmptTimerBizDto.getParam();
        String procName = (String) typeResult.getMap().get("procName");

        //动态调用bean
        Map<String, Object> paramMap = new HashMap<>();
        if (!StringUtils.isNull(param)) {
            paramMap = (Map) JSON.parse(param);
        }

        Object o = SpringContextHolder.getBean("tmptTimerService");
        Method method = o.getClass().getMethod("callProc", String.class, Map.class);
        Result resultObj = (Result) method.invoke(o, procName, paramMap);
        log.info(resultObj.toString());
        log.info(resultObj.getContent());
        return resultObj.getContent();
    }
}
